package com.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import com.app.bean.LoginBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;



public class LoginActivity extends AppCompatActivity {

    private EditText userNameEt;
    private EditText passWordEt;
    private Button loginBt;
    private String username;
    private String password;
    private TextView toRegistTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView(){
        userNameEt = (EditText) findViewById(R.id.username_et);
        passWordEt = (EditText) findViewById(R.id.password_et);
        loginBt = (Button) findViewById(R.id.login_bt);
        toRegistTv = (TextView) findViewById(R.id.to_regist_tv);

        toRegistTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //用户注册页面跳转
                Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
                startActivity(intent);
            }
        });

        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void initData(){
        //获取用户输入的用户名和密码
        username = userNameEt.getText().toString()+"";
        password = passWordEt.getText().toString()+"";
        //将获取的用户名和密码通过json形式传给服务端
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("name",username);//用户名
            jsonObject.put("password",password);//密码
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //创建网络请求
        RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this);
        //发起网络请求
        String url = "http://www.xhjjyst.com/freshmenapp/user/login";//发送请求的地址
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                LoginBean loginBean = gson.fromJson(jsonObject.toString(),LoginBean.class);
                if (loginBean.isSuccess()){
                    //登录成功跳转到主体页面
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    Toast.makeText(LoginActivity.this, loginBean.getMsg()+"", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this, loginBean.getMsg()+"", Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(LoginActivity.this,jsonObject.toString(),Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this,"网络请求错误",Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);//将请求内容放入队列中

    }
}